from Common import Common
import re
from typing import List


class MenuItem(Common):
    '菜单项'

    def __init__(self, data: str = '', parentMenu=None, notes: List[str] = []):
        super().__init__(data, parentMenu, notes)

        pair = self.data.split('|')
        assert len(pair) == 2, '菜单项格式不正确：%s' % self.data

        m = re.match('(.*)\(&(.*)\)', pair[0].strip())
        if m == None:
            self.name = pair[0]
            self.wordsOfName = self._parseNameWords(self.name)
        else:
            self.name = m.group(1)
            self.wordsOfName = self._parseNameWords(m.group(2))

        self.content: str = pair[1]
        '菜单项内容'

        assert len(self.wordsOfName) > 0, '菜单项的名字不能为空：%s' % data

    def isExe(self) -> bool:
        return self.content.__len__() > 4 and self.content[-4: self.content.__len__()] == '.exe'

    def getIni(self) -> str:
        return '%s%s%s(&%s)|%s\n' % (self._getNotesIni(), '\t'*self.parentMenu.level,
                                           self.name, self.getFinalHotkey().upper(), self.content)

    def getCurLevel(self) -> int:
        '获得在快捷键路径上的哪个位置'
        l = 0
        menu = self.parentMenu
        while menu != None and menu.isHotkeyMenu:
            l += 1
            menu = menu.parentMenu

        return l

    def getFinalHotkey(self) -> str:
        '获得最终快捷键'
        if self.numToSameName == -1:
            return self.getHotkey(self.getCurLevel())
        else:
            return self.numToSameName.__str__()